﻿function addAssetType(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var nameInput = bar.find("input[name=name]");
    var src = "/asset/AddAssetType.aspx?parentId="
        +idInput.val() +"&parentName="+nameInput.val();
    $("#detailFrame").attr("src", src);
}

function viewAssetType(node)
{
    var bar = $(node.parentNode).next();
    var idInput = bar.find("input[name=id]");
    var src = "/asset/ViewAssetType.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}

function editAssetType(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var src = "/asset/EditAssetType.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}

function removeAssetType(button)
{
    var bar = $(button.parentNode);
    var idInput = bar.find("input[name=id]");
    var src = "/asset/RemoveAssetType.aspx?id="+idInput.val();
    $("#detailFrame").attr("src", src);
}

function addAsset()
{
    var argsVariable = new Object();
    var answer = window.showModalDialog("/Asset/AddAsset.aspx", argsVariable, 
        "dialogWidth:500px; dialogHeight:500px; dialogLeft:250px; dialogTop:100px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
    if (answer != "cancel")
    {
        var td = $("#assets");
        var newAsset = $("#sampleAsset").clone();
        newAsset.removeAttr("id").css("display", "block");
        newAsset.find(".asset-name").html(answer.name);
        newAsset.find("input[name=assetId]").removeAttr("disabled");
        newAsset.find("input[name=assetName]").val(answer.name).removeAttr("disabled");
        newAsset.find("input[name=assetProductId]").val(answer.productId).removeAttr("disabled");
        newAsset.find("input[name=assetDescription]").val(answer.description).removeAttr("disabled");
        newAsset.find("input[name=assetImportDate]").val(answer.importDate).removeAttr("disabled");
        newAsset.find("input[name=assetTerminateDate]").val(answer.terminateDate).removeAttr("disabled");
        if (answer.imageUrl != "")
        {
            newAsset.find("input[name=assetImageUrl]").val(answer.imageUrl).removeAttr("disabled");
        }
        td.append(newAsset);
        
        var tooltip = newAsset.find(".tooltip-content");
        tooltip.find("strong").html(answer.name);
        tooltip.find("img[name=assetIcon]").attr("src", answer.imageUrl);
        tooltip.find(".divProductId").html(answer.productId);
        tooltip.find(".divImportDate").html(answer.importDate);
        tooltip.find(".divTerminateDate").html(answer.terminateDate);
        tooltip.find(".divDescription").html(answer.description);
    }
}

function editAsset(button)
{
    var bar = $(button.parentNode);
    var obj = new Object();
    obj.name = bar.find("input[name=assetName]").val();
    obj.productId = bar.find("input[name=assetProductId]").val();
    obj.description = bar.find("input[name=assetDescription]").val();
    obj.importDate = bar.find("input[name=assetImportDate]").val();
    obj.terminateDate = bar.find("input[name=assetTerminateDate]").val();
    obj.imageUrl = bar.find("input[name=assetImageUrl]").val();
    
    var answer = window.showModalDialog("/Asset/EditAsset.aspx", obj, 
        "dialogWidth:500px; dialogHeight:500px; dialogLeft:250px; dialogTop:100px; center:yes;resizable:no;status:no; statusbar:no; toolbar:no; menubar:no; addressbar:no; titlebar:no;");
        
    if (answer != "cancel")
    {
        var newAsset = $(button.parentNode.parentNode);
        newAsset.find(".asset-name").html(answer.name);
        newAsset.find("input[name=assetName]").val(answer.name);
        newAsset.find("input[name=assetProductId]").val(answer.productId);
        newAsset.find("input[name=assetDescription]").val(answer.description);
        newAsset.find("input[name=assetImportDate]").val(answer.importDate);
        newAsset.find("input[name=assetTerminateDate]").val(answer.terminateDate);
        if (answer.imageUrl != "")
        {
            newAsset.find("input[name=assetImageUrl]").val(answer.imageUrl);
        }
        
        var tooltip = $(button.parentNode.parentNode).find(".tooltip-content");
        tooltip.find("strong").html(answer.name);
        tooltip.find("img[name=assetIcon]").attr("src", answer.imageUrl);
        tooltip.find(".divProductId").html(answer.productId);
        tooltip.find(".divImportDate").html(answer.importDate);
        tooltip.find(".divTerminateDate").html(answer.terminateDate);
        tooltip.find(".divDescription").html(answer.description);
    }
}

function removeAsset(button)
{
    var row = $(button.parentNode.parentNode);
    var assetId = $(button.parentNode).find("input[name=assetId]");
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Asset/AjaxAsset2.aspx",
	    data : { 
		    "operation" : "remove_asset", 
		    "assetId" : assetId.val()
	    }, 
	    success : function (r) {
			if(r.error.length > 0)
			{
			    var error = "";
			    for (var i=0; i<r.error.length; i++)
			    {
			        error += r.error[i] + "\n";
			    }
			    alert(error);
			    if(r.status)
			    {
			        row.remove();
			    }
			}
			else
			{
			    row.remove();
			}
	    }
    })
    .always(function() {
	    unmask(area);
	});
}


function editAsset(button, mode)
{
    var row = $(button).closest(".div-row");
    var cells = row.find(".div-cell");
    if (cells.eq(1).find("input[type=text]").length > 0)
    {
        processUpdate(button);
    }
    
    var divName = cells.eq(1).find("div");
    var input = "";
    if (mode == "add")
    {
        input += "<input type=\"hidden\" name=\"assetId\" value=\"\" />";
    }
    input += "<input type=\"text\" name=\"assetName\" value=\""+divName.text()+"\" onkeydown=\"enter(this, event);\"/>";
    if (divName.length > 0)
    {
        divName.eq(0).remove();
    }
    if (cells.eq(1).find("input[type=text]").length < 1)
    {
        cells.eq(1).append(input);
    }
    
    
    if (cells.eq(2).find("input[type=text]").length < 1)
    {
        input = "<input type=\"text\" name=\"description\" value=\""+cells.eq(2).text()+"\" onkeydown=\"enter(this, event);\"/>";
        cells.eq(2).html(input);
    }
    if (cells.eq(3).find("input[type=text]").length < 1)
    {
        randomId = "tbx" + randomString();
        input = "<input type=\"text\" id=\""+randomId+"\" name=\"startDate\" value=\""+cells.eq(3).text()+"\" onkeydown=\"enter(this, event);\"/>";
        input += "<script type=\"text/javascript\" language=\"javascript\">new JsDatePick({useMode:2,target:\""+randomId+"\",dateFormat:\"%d/%m/%Y\"});</script>";
        cells.eq(3).html(input);
    }
    if (cells.eq(4).find("input[type=text]").length < 1)
    {
        randomId = "tbx" + randomString();
        input = "<input type=\"text\" id=\""+randomId+"\" name=\"endDate\" value=\""+cells.eq(4).text()+"\" onkeydown=\"enter(this, event);\"/>";
        input += "<script type=\"text/javascript\" language=\"javascript\">new JsDatePick({useMode:2,target:\""+randomId+"\",dateFormat:\"%d/%m/%Y\"});</script>";
        cells.eq(4).html(input);
    }
    
    if (mode=="add" || mode=="enter")
    {
        var table = row.parent();
        var newRow = "<div class=\"div-row\">";
        newRow += "<div class=\"div-cell c0\"></div>";
        newRow += "<div class=\"div-cell c1\">";
        newRow += "<span class=\"add\" onclick=\"editAsset(this, 'add');\"></span>";
        newRow += "<span class=\"remove\" onclick=\"removeAsset(this);\" style=\"visibility:hidden\"></span>";
        newRow += "<input type=\"hidden\" name=\"assetId\" value=\"\" />";
        newRow += "<div></div>";
        newRow += "</div>";
        newRow += "<div class=\"div-cell c4\"></div>";
        newRow += "<div class=\"div-cell c2\"></div>";
        newRow += "<div class=\"div-cell c2\"></div>";
        newRow += "</div>";
        table.append(newRow);
        
        var addButton = cells.eq(1).find(".add");
        if (addButton.length > 0)
        {
            addButton.eq(0).attr("class", "edit");
            addButton.eq(0).attr("onclick", "editAsset(this, 'edit');");
        }
        var enterButton = cells.eq(1).find(".remove");
        if (enterButton.length > 0)
        {
            enterButton.eq(0).css("visibility", "visible");
        }
    }
    cells.eq(1).find("input[type=text]").focus();
}


function processUpdate(input)
{
    var row = $(input).closest(".div-row");
    var cells = row.find(".div-cell");
    var inputs = row.find("input[type=text]");
    var hiddens = row.find("input[type=hidden]");
    var divName = "<div>"+inputs.eq(0).val()+"</div>";
    var assetTypeId = $("a.jstree-clicked").closest("li").attr("id");
    
    var area = mask();
    $.post(
		"/Asset/AjaxAsset2.aspx", 
		{
		    "assetTypeId" : assetTypeId,
		    "assetId" : hiddens.eq(0).val(), 
			"assetName" : inputs.eq(0).val(), 
			"description" : inputs.eq(1).val(), 
			"startDate" : inputs.eq(2).val(),
			"endDate" : inputs.eq(3).val()
		}, 
		function (r) {
		    if(r.status)
			{
			    hiddens.eq(0).val(r.assetId);
			}
			if(r.error.length > 0)
			{
			    var error = "";
			    for (var i=0; i<r.error.length; i++)
			    {
			        error += r.error[i] + "\n";
			    }
			    alert(error);
			}
			else {
				cells.eq(1).append(divName);
                cells.eq(2).text(inputs.eq(1).val());
                cells.eq(3).text(inputs.eq(2).val());
                cells.eq(4).text(inputs.eq(3).val());
                inputs.eq(0).remove();
			}
		}
	)
	.always(function() {
	    unmask(area);
	});
}


function enter(input, evt)
{
    if (evt.keyCode != 13)
    {
        return false;
    }
    processUpdate(input);
    
    return true;
}


function selectTreeNode(assetTypeId)
{
    var area = mask();
    $.post(
		"/Asset/AjaxAsset2.aspx", 
		{ 
			"operation" : "select_node", 
			"assetTypeId" : assetTypeId
		}, 
		function (r) {
		    var table = $(".div-table");
	        var rows = table.find(".div-row");
	        for (var j=rows.length-2; j>0; j--)
	        {
                rows.eq(j).remove();
	        }
	        var header = rows.eq(0);
	        var fm = createFormat(r.assets.length);
		    for (var i=r.assets.length-1; i>=0; i--)
		    {
		        var newRow = "<div class=\"div-row\">";
		        newRow += "<div class=\"div-cell c0\">"+r.assets[i].order+"</div>";
                newRow += "<div class=\"div-cell c1\">";
                newRow += "<span class=\"edit\" onclick=\"editAsset(this, 'edit');\"></span>";
                newRow += "<span class=\"remove\" onclick=\"removeAsset(this);\"></span>";
                newRow += "<input type=\"hidden\" name=\"assetId\" value=\""+r.assets[i].assetId+"\" />";
                newRow += "<div>"+r.assets[i].assetName+"</div>";
                newRow += "</div>";
                newRow += "<div class=\"div-cell c4\">"+r.assets[i].description+"</div>";
                newRow += "<div class=\"div-cell c2\">"+r.assets[i].startDate+"</div>";
                newRow += "<div class=\"div-cell c2\">"+r.assets[i].endDate+"</div>";
                newRow += "</div>";
                $(newRow).insertAfter(header);
		    }
		}
	)
	.fail(function() { alert("error"); })
	.always(function() {
	    unmask(area);
	});
}